<?php 
class special
{
	var $db;
	function special()
	{
		global $db;	
		$this -> db     = & $db;
	}
	//添加活动
	function add_special()
	{
		$sql="INSERT INTO ".SPECIAL." (`title` ,`desc` , `start_time` ,`end_time` ,`create_user` ,`status`,`displayorder`,`pic`,`templates`,`type`,`nums`,`type_config`,`catid`,`cpns_event_key`,`bgcolor`,`seo_title`,`seo_keywords`,`seo_description`)VALUES ( '$_POST[title]','$_POST[desc]','".strtotime($_POST['stime'])."','".strtotime($_POST['etime'])."','".$_SESSION[ADMIN_USER]."','$_POST[status]','0','$_POST[pic]','$_POST[templates]','$_POST[type]','$_POST[nums]','$_POST[type_config]','$_POST[catid]','{$_POST['cpns_event_key']}','{$_POST['bgcolor']}','{$_POST['seo_title']}','{$_POST['seo_keywords']}','{$_POST['seo_description']}')";
		$this->db->query($sql);
		$res = $this->db->lastid();
		if($res>0 && $_POST['new_floor']){
			$values = "";
			foreach ($_POST['new_floor'] as $key=>$f)
			{
				if($f){
					$values .= "('{$f}',{$res},'{$_POST['new_fdisplayorder'][$key]}','{$_POST['new_fbgcolor'][$key]}'),";
				}
			}
			if($values!=''){
				$sql = "INSERT INTO ".SPECIALPRODUCTFLOOR." (`title`,`special_id`,`displayorder`,`bgcolor`) VALUES ".rtrim($values,",");
				$this->db->query($sql);
			}
		}
	}
	//修改活动
	function edit_special()
	{
		$sql="UPDATE ".SPECIAL."  SET `title` = '$_POST[title]',`desc` = '$_POST[desc]',`start_time` =  '".strtotime($_POST['stime'])."',`end_time` = '".strtotime($_POST['etime'])."',`pic` = '$_POST[pic]',`status` = '$_POST[status]',`templates`='$_POST[templates]',`type`='$_POST[type]',`nums`='$_POST[nums]',`type_config`='$_POST[type_config]',`catid`='$_POST[catid]',`cpns_event_key`='{$_POST['cpns_event_key']}',`bgcolor`='{$_POST['bgcolor']}',`seo_title`='{$_POST['seo_title']}',`seo_keywords`='{$_POST['seo_keywords']}',`seo_description`='{$_POST['seo_description']}'  WHERE `id` ='$_POST[id]'  ";	
		$this->db->query($sql);
		
		$sql ="update ".SPECIALPRODUCT. " set `status`='$_POST[status]'  where activity_id= '$_POST[id]'";
		
		$this->db->query($sql);
	}
	//获取所有活动列表
	function get_special_list()
	{
		$sql="select id,title,start_time,end_time from ".SPECIAL." where status>0 ";
		$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$this->db->query($sql);
			$page->totalRows =$this->db->num_rows();
		}
		$sql .= "  limit ".$page->firstRow.",20";
		//=====================
		$this->db->query($sql);
		$re['list']=$this->db->getRows();
		$re['page']=$page->prompt();	
		return $re;
	}
	
	function save_special_product()
	{
		global $buid;
		$id=$_POST['id']*1;

		if(!empty($_POST['chk']))
		{
			foreach($_POST['chk'] as $val)
			{
				$sql="select id from ".SPECIALPRODUCT." where product_id='$val' and activity_id=$id";
				$this->db->query($sql);
				$aid=$this->db->fetchField('id');
				if(!$aid)
				{
					$sql="select id,title,start_time,end_time,status from ".SPECIAL." where id=$id ";
					$this->db->query($sql);
					$pinfo=$this->db->fetchRow();
					
					$sql="INSERT INTO ".SPECIALPRODUCT."(`activity_id` ,`product_id` ,`status` ,`create_user`) VALUES ('$id', '$val', '$pinfo[status]','".$_SESSION[ADMIN_USER]."')";
					$re=$this->db->query($sql);
				}
			}
		}
		return $re;
	}
}
?>